<template>
  <div class="app-container">
    <div class="filter-container">
      <label for="search" class="filter-item" style="font-size: 12px;">
        文件名：
      </label>
      <el-input
        id="search"
        v-model="query.name"
        size="mini"
        clearable
        placeholder="请输入文件名"
        style="width: 200px;margin-right: 10px"
        class="filter-item"
        @keyup.enter.native="getList"
      />
      <el-select v-model="query.type" style="margin-right: 10px" class="filter-item" value="" size="mini">
        <el-option
          v-for="item in fileType"
          :key="item.key"
          :label="item.label"
          :value="item.key"
        />
      </el-select>
      <el-button
        size="mini"
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="getList"
      >查询
      </el-button>
      <el-button
        size="mini"
        type="primary"
        style="margin-left: 5px"
        icon="el-icon-plus"
        class="filter-item"
        @click="addDialog"
      >添加
      </el-button>
    </div>
    <file-table
      :list="list"
      :loading="loading"
      :total="total"
      :query="query"
      style="width: 880px;"
      @edit="edit"
      @reload="getList"
    />

    <el-dialog v-if="dialog" :visible.sync="dialog" :title="title" :width="'650px'">
      <file-dialog
        :form="form"
        @create="create"
        @close="dialog = false"
        @reload="getList"
      />
    </el-dialog>
  </div>
</template>

<script>
import fileTable from './module/fileTable'
import fileDialog from './module/fileDialog'
import { fetchFileList } from '../../api/file'
export default {
  name: 'File',
  components: { fileTable, fileDialog },
  data() {
    return {
      loading: true,
      dialog: false,
      title: '',
      total: 0,
      list: [],
      fileType: [
        { label: '所有类型', key: 'all' },
        { label: 'Scratch文件', key: 'sb3' },
        { label: 'Python文件', key: 'py' }
      ],
      query: {
        page: 1,
        limit: 8,
        name: null,
        type: 'all',
        sort: '+id'
      },
      form: {
        id: null,
        work_id: null,
        name: '',
        pageURL: '',
        fileURL: '',
        type: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      fetchFileList(this.query).then(response => {
        this.$SUCCESS('获取文件列表成功')
        if (response.data.total !== 0) {
          this.list = response.data.items
          for (const i in this.list) {
            this.list[i].num = (this.query.page - 1) * this.query.limit + parseInt(i) + 1
          }
          this.total = response.data.total
        } else {
          this.list = []
        }
        this.loading = false
      }).catch(() => {
        this.$ERROR('获取文件列表失败')
        this.loading = false
      })
    },
    resetForm() {
      this.form = this.$options.data().form
    },
    create(item) {
      this.list.unshift(item)
      for (const i in this.list) {
        this.list[i].num = (this.query.page - 1) * this.query.limit + parseInt(i) + 1
      }
    },
    edit(item) {
      this.resetForm()
      this.$setDialog(true, '编辑文件', true)
      this.form = JSON.parse(JSON.stringify(item))
    },
    addDialog() {
      this.resetForm()
      this.$setDialog(true, '添加文件', false)
    }
  }
}
</script>

<style scoped>

</style>
